package com.jack.vipmis.vip.api.web.controller;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.entity.CardEntity;
import com.jack.vipmis.vip.api.util.MsgUtil;
import com.jack.vipmis.vip.card.entity.Card;
import com.jack.vipmis.vip.card.service.CardService;

/**
 * 会员卡信息控制器
 * 
 * @author JackLiang
 * @date 2014-7-7
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/api/card")
public class CardApiController extends BaseController<Card, Long> {
	private static final Logger	LOG	= LoggerFactory.getLogger(CardApiController.class);

	@Autowired
	private CardService			cardService;

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "find", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findById(@RequestParam("id") long id) {
		Card m = cardService.findOne(id);
		if (m != null) {
			return MsgUtil.doneOk(cardService.findOne(id));
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findByPage", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findByPage(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize,
			HttpServletRequest request) {
		try {
			// 获取参数
			String cardType = request.getParameter("cardType").intern();
			String cardClass = request.getParameter("cardClass").intern();
			String cnd = request.getParameter("cnd").intern();
			String x = request.getParameter("x").intern();
			String y = request.getParameter("y").intern();
			String text = request.getParameter("text") == null ? "" : //
					URLDecoder.decode(request.getParameter("text").intern(), "utf-8");

			LOG.info("text's content is:", text);

			long tasteId = Long.valueOf(cardClass);

			Page<Card> result = cardService.getCardByPage(page, pageSize, cnd, tasteId, cardType, x, y, text);

			List<Card> list = result.getContent();
			List<CardEntity> returnList = Lists.newArrayList();
			for (int i = 0, len = list.size(); i < len; i++) {
				CardEntity card = new CardEntity();
				card.setBsId(list.get(i).getBusiness() == null ? 0L : list.get(i).getBusiness().getId());
				card.setBusiness(list.get(i).getBusiness());
				card.setCardAddress(list.get(i).getCardAddress() == null ? "" : list.get(i).getCardAddress());
				card.setCardClass(list.get(i).getCardTaste() == null ? 0L : list.get(i).getCardTaste().getId());
				card.setCardCreateTime(list.get(i).getCardCreateTime());
				card.setCardDownLoadCount(list.get(i).getCardDownLoadCount() == null ? 0 : list.get(i)
						.getCardDownLoadCount());
				card.setCardLogo(list.get(i).getCardLogo() == null ? "" : list.get(i).getCardLogo());
				card.setCardName(list.get(i).getCardName() == null ? "" : list.get(i).getCardName());
				card.setCardSaleCode(list.get(i).getCardSaleCode() == null ? "" : list.get(i).getCardSaleCode());
				card.setCardSpecialTips(list.get(i).getCardSpecialTips() == null ? "" : list.get(i)
						.getCardSpecialTips());
				card.setCardSpecialTipsContent(list.get(i).getCardSummaryContent() == null ? "" : list.get(i)
						.getCardSpecialTipsContent());
				card.setCardSummary(list.get(i).getCardSummary() == null ? "" : list.get(i).getCardSummary());
				card.setCardSummaryContent(list.get(i).getCardSummaryContent() == null ? "" : list.get(i)
						.getCardSummaryContent());
				card.setCardType(list.get(i).getCardType() == null ? "" : list.get(i).getCardType());
				card.setCardValidityTime(list.get(i).getCardValidityTime() == null ? 0L : list.get(i)
						.getCardValidityTime());
				card.setCardZone(list.get(i).getCardZone() == null ? "" : list.get(i).getCardZone());
				card.setDistanceX(list.get(i).getDistanceX() == null ? "" : list.get(i).getDistanceX());
				card.setDistanceY(list.get(i).getDistanceY() == null ? "" : list.get(i).getDistanceY());
				card.setId(list.get(i).getId());

				returnList.add(card);
			}
			return MsgUtil.doneOk(returnList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}
}
